package com.dengyuanke.admin.controller;


import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.Admin;
import com.dengyuanke.entity.FileUpload;
import com.dengyuanke.service.IFileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 文件上传记录 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-09
 */
@Api(tags = "文件上传相关API")
@RestController
public class FileController {
    @Autowired
	private IFileUploadService fileUploadService;

    @ApiOperation(value = "上传文件",notes = "传入file文件流和type文件类型，(1,商品略缩图), (2,商品详情图), (3,轮播图);")
    @PostMapping("/uploadfile")
    public JsonResult uploadFile(@ApiIgnore @RequestAttribute Admin currentUser, @RequestParam("file") CommonsMultipartFile file, HttpServletRequest request,int type)throws IOException{
        if (!file.isEmpty()) {
            FileUpload fileUpload=new FileUpload();
            fileUpload.setId(IdWorker.get32UUID());
            fileUpload.setCreateTime(new Date());
            fileUpload.setCreatorId(currentUser.getId());
            fileUpload.setOriginalname(file.getOriginalFilename());
            // 取文件格式后缀名
            String fileExt= file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
            fileUpload.setExt(fileExt);
            // 取当前时间戳作为文件名
            String newFileName = System.currentTimeMillis() + fileExt;
            fileUpload.setName(newFileName);
            String path=null;
            // (1,"商品略缩图"), (2,"商品详情图"), (3,"轮播图");
            if(1==type){
                // 存放位置
                path = request.getSession().getServletContext().getRealPath( File.separator+"resources") +  File.separator+"img"+ File.separator+"goodsthumb"+ File.separator+newFileName;
                fileUpload.setAbsolutePath(path);
                fileUpload.setRelativePath(File.separator+"resources" +  File.separator+"img"+ File.separator+"goodsthumb"+ File.separator+newFileName);
            }
            if(2==type){
                // 存放位置
                path = request.getSession().getServletContext().getRealPath( File.separator+"resources") +  File.separator+"img"+ File.separator+"goodsdetails"+ File.separator+newFileName;
                fileUpload.setAbsolutePath(path);
                fileUpload.setRelativePath(File.separator+"resources" +  File.separator+"img"+ File.separator+"goodsdetails"+ File.separator+newFileName);
            }
            if(3==type){
                // 存放位置
                path = request.getSession().getServletContext().getRealPath( File.separator+"resources") +  File.separator+"img"+ File.separator+"carousel"+ File.separator+newFileName;
                fileUpload.setAbsolutePath(path);
                fileUpload.setRelativePath(File.separator+"resources" +  File.separator+"img"+ File.separator+"carousel"+ File.separator+newFileName);
            }

            File newFile = new File(path);
            if(!newFile.exists()){
                newFile.mkdirs();
            }
            // 复制临时文件到指定目录下
            file.transferTo(newFile);
            fileUpload.setBusinessType(type);
            boolean insert = fileUploadService.insert(fileUpload);
            if(insert){
                return JsonResult.renderSuccess((Object) fileUpload.getRelativePath());
            }else {
                return JsonResult.renderFail("上传失败");
            }
        } else {
                return JsonResult.renderFail("上传文件为空，上传失败");
            }
    }

}
